<template>
  <!-- 弹出框通用模版 -->
  <div class="add-edit">
    <div class="add-edit-tittle">
      <p class="tittle-text">{{ pageData.AddEditText }}</p>
      <div class="tittle-button">
        <el-button size="mini" @click="_goBack">返回</el-button>
      </div>
    </div>
    <el-form
      ref="ruleForm"
      :model="ruleForm"
      :rules="rules"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="名称" prop="name">
        <el-input v-model="ruleForm.name" />
      </el-form-item>

      <el-form-item label="所在城市" prop="region">
        <el-select v-model="ruleForm.region" placeholder="请选择活动区域">
          <el-option label="上海" value="shanghai" />
          <el-option label="北京" value="beijing" />
        </el-select>
      </el-form-item>

      <el-form-item label="时间选择" required>
        <el-date-picker
          v-model="ruleForm.date"
          type="datetime"
          placeholder="选择日期时间"
        />

      </el-form-item>

      <el-form-item label="开关按钮" prop="delivery">
        <el-switch v-model="ruleForm.delivery" />
      </el-form-item>

      <el-form-item label="兴趣爱好" prop="type">
        <el-checkbox-group v-model="ruleForm.type">
          <el-checkbox label="唱歌" name="type" />
          <el-checkbox label="跳舞" name="type" />
          <el-checkbox label="打游戏" name="type" />
          <el-checkbox label="emo大神" name="type" />
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="ruleForm.sex">
          <el-radio label="男" />
          <el-radio label="女" />
        </el-radio-group>
      </el-form-item>
      <el-form-item label="简介" prop="desc">
        <el-input v-model="ruleForm.desc" type="textarea" />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="submitForm('ruleForm')"
        >保存</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: {
    // 数据列表 数据共 XX 条
    pageData: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      ruleForm: {
        name: '', // 名称
        region: '', // 所在城市
        date1: '', // 时间
        delivery: false, // 开关
        type: [], // 兴趣爱好
        sex: '', // 性别
        desc: '' // 简介
      },
      // 表单验证
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 3, max: 9, message: '长度在 3 到 9 个字符', trigger: 'blur' }
        ],
        region: [
          { required: true, message: '请选择所在城市', trigger: 'change' }
        ],
        date1: [
          {
            type: 'date',
            required: true,
            message: '请选择日期',
            trigger: 'change'
          }
        ],
        type: [
          {
            type: 'array',
            required: true,
            message: '请至少选择一个兴趣爱好',
            trigger: 'change'
          }
        ],
        sex: [
          { required: true, message: '请选择性别', trigger: 'change' }
        ],
        desc: [{ required: true, message: '请填写简介', trigger: 'blur' }]
      },

      formData: null
    }
  },
  mounted() {
    this.formData = this.pageData
  },
  methods: {
    // 保存 按钮事件
    submitForm(formName) {
      // 表单验证是否通过的验证
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(this.ruleForm, '表单数据')
          console.log('保存成功', this.pageData.AddEdit)
          if (this.pageData.AddEdit === 'add') {
            console.log('新增保存')
          }
          if (this.pageData.AddEdit === 'edit') {
            console.log('编辑保存')
          }
        } else {
          console.log('保存失败', this.pageData.AddEdit)
          return false
        }
      })
    },
    // 重置按钮事件
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },

    // 返回按钮事件
    _goBack() {
      this.formData.popUpFlag = false
    }
  }
}
</script>

<style lang="scss" scoped>
.add-edit {
  height: 700px;
  width: 900px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  overflow-y: scroll;
  background-color: #FFFFFF;
  .add-edit-tittle {
    height: 100px;
    width: 100%;
    padding: 0 10px;
    background-color: orange;

    display: flex;
    justify-content: space-between;
    align-items: center;

    position: fixed;
    top: 0;
    z-index: 5;
    .tittle-text {
      font-size: 18px;
      font-weight: bold;
    }
  }
  .el-form {
    padding: 110px 10px 10px 10px;
  }
}
</style>
